<template>
  <div>
    <!-- 未登录的头部 -->
    <div class="header not-login" v-if="!user">
      <div class="login-btn" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" alt="" class="mobile-img" />
        <span class="text">登录/注册</span>
      </div>
    </div>
    <!-- 已登录头部 -->
    <div class="header" v-else>
      <div class="user-info">
        <div class="left">
          <van-image
            round
            class="avatar"
            fit="cover"
            :src="userInfo.photo"
          />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button size="mini" round to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-status">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span><span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span><span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span><span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span><span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- 导航 -->
    <van-grid :column-num="2" class="grid-nav mb-9" clickable>
      <van-grid-item class="grid-item">
        <!-- 方式一:作用域插槽,v-solt: -->
        <!-- <template v-solt:icon> -->
        <!-- 方式二:作用域插槽,v-solt:=>#icon -->
        <!-- <template #icon> -->
        <!-- 方式三: solt="icon"-->
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link />
    <van-cell class="mb-9" title="小智同学" is-link />
    <van-cell
      v-if="user"
      @click="onLoginout"
      title="退出"
      class="logout-cell"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  name: 'MyPage',
  props: [],
  components: {},
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  async created () {
    const res = await getUserInfo()
    this.userInfo = res.data.data
  },
  mounted () {},
  methods: {
    async onLoginout () {
      try {
        await this.$dialog.confirm({
          title: '提示',
          message: '确认退出吗'
        })
        console.log('点击了确认')
        this.$store.commit('setUser', null)
        this.$router.push('/login')
      } catch (error) {
        console.log('点击取消')
      }
    }
  },
  watch: {}
}
</script>

<style lang="less" scoped>
.header {
  background-image: url("~@/assets/banner.png");
  height: 361px;
  background-size: cover;
}
.not-login {
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-btn {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
}
.mobile-img {
  width: 132px;
  height: 132px;
  margin-bottom: 15px;
}
.text {
  font-size: 28px;
  color: #fff;
}
.user-info {
  display: flex;
  justify-content: space-between;
  align-content: center;
  height: 231px;
  padding: 76px 32px 23px;
  box-sizing: border-box;
  .left {
    display: flex;
    align-items: center;
  }
  .avatar {
    width: 132px;
    height: 132px;
    border: 4px solid #fff;
    margin-right: 23px;
  }
  .name {
    font-size: 30px;
    color: #fff;
  }
}
.data-status {
  display: flex;
  justify-content: space-around;
  align-items: center;
  .data-item {
    height: 130px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #fff;
    .count {
      font-size: 46px;
    }
    .text {
      font-size: 23px;
    }
  }
}
.grid-nav {
  .grid-item {
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    span {
      font-size: 28px;
    }
  }
}
.logout-cell {
  text-align: center;
  color: #d86262;
}

.mb-9 {
  margin-bottom: 9px;
}
</style>
